/*
 * Principale.c
 *
 *  Created on: 20 oct. 2011
 *      Author: alex
 */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <SDL/SDL.h>

#include "Principale.h"
#include "Sudoku.h"
#include "Sdl.h"

int main( void ){
	/*					CREATION DES VARIABLES 						*/
	Sudoku_s *sudoku = NULL, *memoire_plein = NULL, *memoire_vide = NULL;
	SDL_Surface *ecran = NULL;
	SDL_Event evenement;
	SDL_Rect position;
	SDL_Surface **img_gris = NULL, **img_vert = NULL, **img_blanc = NULL, **img_vertF = NULL, **img_marron = NULL;
	int retour;

	/* 					INITIALISATION DES VARIABLES				*/
	sudoku = creer_sudoku_std( );
	srand( time(NULL) );
	ecran = initSDL();
	init_cases( &img_gris, &img_blanc, &img_vert, &img_vertF, &img_marron );

	/*					PROGRAMME									*/

	construit_grille3( sudoku );
	memoire_plein = copy_sudoku( sudoku );
#ifdef DEBUG
	printf("Grille rempli:\n");
	print_Sudoku( sudoku );
#endif

	troue_grille( sudoku );
	memoire_vide = copy_sudoku( sudoku );
#ifdef DEBUG
	printf("Apres suppression de quelques trous:\n");
#endif
	print_Sudoku( sudoku );
	dessine_grille( ecran, img_gris, img_marron[0], sudoku);

	/*					DEBUT PARTIE								*/
	while( SDL_WaitEvent( &evenement ) ){
		switch( evenement.type ){
		case SDL_QUIT:
			libere_sudoku( &sudoku );
			libere_sudoku( &memoire_plein );
			libere_sudoku( &memoire_vide );
			SDL_Quit();
			return EXIT_SUCCESS;
		case SDL_MOUSEBUTTONDOWN:
			position.x = evenement.button.x;
			position.y = evenement.button.y;

			if( position.x < 360 && position.x >= 0 && position.y >= 0 && position.y < 360 ){
				position.x = (position.x/40)*40;
				position.y = (position.y/40)*40;
				retour = change_valeur( ecran, img_gris, img_vert, img_marron, img_marron, &position, sudoku);
			}

			if( retour == -1 ){
				libere_sudoku( &sudoku );
				libere_sudoku( &memoire_plein );
				libere_sudoku( &memoire_vide );
				SDL_Quit();
				return EXIT_SUCCESS;
			}
			if( retour != -3 && memoire_plein->tab[position.y/40][position.x/40] == retour ){
				sudoku->tab[position.y/40][position.x/40] = retour;
				place_nombre(ecran, img_gris, &position, retour);
			}

			break;
		}
	}

	/*					LIBERATION DES VARIABLES					*/
	libere_sudoku( &sudoku );
	libere_sudoku( &memoire_plein );
	libere_sudoku( &memoire_vide );
	SDL_Quit();
	return EXIT_SUCCESS;
}

int change_valeur( SDL_Surface *ecran, SDL_Surface **gris, SDL_Surface **vert, SDL_Surface **vertF, SDL_Surface **marron, SDL_Rect *position, Sudoku_s *grille ){
	SDL_Event evenement;
	int valeur = -2;
	peint_lettre(ecran, vertF);
	colorie_grille( ecran, vert, marron, grille, position);

	while( SDL_WaitEvent( &evenement ) && valeur == -2){
		switch( evenement.type ){
		case SDL_QUIT:
			return -1;
		case SDL_MOUSEBUTTONDOWN:
			switch( evenement.button.button ){
			case SDL_BUTTON_LEFT:
				if( evenement.button.x >= 360 && evenement.button.x <= 400 )
					valeur = (evenement.button.y/40)+1;
				else{
					position->x = (evenement.button.x/40)*40;
					position->y = (evenement.button.y/40)*40;
					remove_color( ecran, gris, grille );
					colorie_grille( ecran, vert, marron, grille, position);
					SDL_Flip( ecran );
				}
				break;
			case SDL_BUTTON_RIGHT:
				valeur = -3;
			}
			break;
		case SDL_KEYDOWN:
			switch( evenement.key.keysym.sym){
			case SDLK_UP:
				remove_color( ecran, gris, grille );
				if( position->y >= 40 )
					position->y-=40;
				colorie_grille( ecran, vert, marron, grille, position);
				break;
			case SDLK_DOWN:
				remove_color( ecran, gris, grille );
				if( position->y < 320)
					position->y+=40;
				colorie_grille( ecran, vert, marron, grille, position);
				break;
			case SDLK_LEFT:
				remove_color( ecran, gris, grille );
				if( position->x >= 40)
					position->x-=40;
				colorie_grille( ecran, vert, marron, grille, position);
				break;
			case SDLK_RIGHT:
				remove_color( ecran, gris, grille );
				if( position->x < 320 )
					position->x+=40;
				colorie_grille( ecran, vert, marron, grille, position);
				break;
			case SDLK_ESCAPE:
				valeur = -3;
				break;
			case SDLK_KP1:
				valeur = 1;
				break;
			case SDLK_KP2:
				valeur = 2;
				break;
			case SDLK_KP3:
				valeur = 3;
				break;
			case SDLK_KP4:
				valeur = 4;
				break;
			case SDLK_KP5:
				valeur = 5;
				break;
			case SDLK_KP6:
				valeur = 6;
				break;
			case SDLK_KP7:
				valeur = 7;
				break;
			case SDLK_KP8:
				valeur = 8;
				break;
			case SDLK_KP9:
				valeur = 9;
				break;
			}
		}
	}

	remove_droite( ecran, marron[0] );
	remove_color( ecran, gris, grille );
	SDL_Flip( ecran );
	return valeur;
}

int calcul_nbre_tour( const Sudoku_s const * sudoku ){
	int i,j;

	int nbre_tour = 0;

	for( i = 0; i < sudoku->hauteur; i++ ){
		for( j = 0; j < sudoku->largeur; j++ ){
			if( sudoku->tab[i][j] == 0 )
				nbre_tour++;
		}
	}

	return nbre_tour;
}

void joue ( const Sudoku_s const *sudoku_plein, Sudoku_s *sudoku_vide ) {
	int x,y, proposition;
	int nbreTour = calcul_nbre_tour( sudoku_vide );
	time_t debut, fin;

	debut = time(NULL);
	while( nbreTour != 0 ){
		nbreTour--;
		do{
			printf("x:\t");
			x = demande_lettre()-49;
			while( demande_lettre() != '\n' ){
			}
			printf("y:\t");
			y = demande_lettre()-49;

			while( demande_lettre() != '\n' ){
			}
		}while( x < 0 || x > 8 || y < 0 && y > 8  || sudoku_vide->tab[y][x] != 0);

		do{
			printf("valeur:\t");
			proposition = demande_lettre()-48;

			while( demande_lettre() != '\n' ){
			}
		}while( proposition < 1 || proposition > 9 );

		system("clear");
		if( sudoku_plein->tab[y][x] == proposition ){
			sudoku_vide->tab[y][x] = proposition;
			printf("Exacte!\n");
		}else{
			nbreTour++;
			printf("Fausse proposition!\n");
		}

		print_Sudoku( sudoku_vide );
	}

	fin = time(NULL);
	printf("Temps: ");
	print_temps( difftime( fin, debut) );
	printf("\n");
	printf("Felicitation vous avez reussi!\n");
}

void print_temps( const double time ){
	int minute = (int)time / 60;
	int seconde = (int)time % 60;
	int heure = minute / 60;
	minute = minute % 60;

	printf("%dh %dmin %ds", heure, minute, seconde);
}
